package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author 黑马程序员
 * @since 2022-06-06
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

//    @Autowired
//    private ShoppingCartService shoppingCartService;

//
//    public ShoppingCart saveByShoppingCart(ShoppingCart shoppingCart) {
////        log.info("购物车信息+{}",shoppingCart);
////        //首先获取当前用户登录id
////        Long currentId = BaseContext.getCurrentId();
////        //把用户id存入到购物车数据库中
////        shoppingCart.setUserId(currentId);
////        LambdaQueryWrapper<ShoppingCart> qw =new LambdaQueryWrapper<>();
////        qw.eq(ShoppingCart::getUserId,currentId);
////        Long dishId = shoppingCart.getDishId();
////        Long setmealId = shoppingCart.getSetmealId();
////
////        if (dishId==null){
////            //说明是套餐分类
////            qw.eq(ShoppingCart::getSetmealId,setmealId);
////        }else{
////            //说明是菜品分类
////            qw.eq(ShoppingCart::getDishId,dishId);
////        }
////        //查询当前菜品是否在购物车中
////        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
////        ShoppingCart shoppingOne = shoppingCartService.getOne(qw);
////        if (shoppingOne!=null){
////            //说明在购物车中
////            Integer number = shoppingCart.getNumber();
////            shoppingOne.setNumber(number+1);
////            shoppingCartService.updateById(shoppingOne);
////        }else {
////            //如果不存在，则添加到购物车，数量默认就是一
////            shoppingCart.setNumber(1);
////            shoppingCart.setCreateTime(LocalDateTime.now());
////            shoppingCartService.save(shoppingCart);
////            shoppingOne=shoppingCart;
////        }
////        return (shoppingOne);
//
//        log.info("购物车数据:{}",shoppingCart);
//
//        //设置用户id，指定当前是哪个用户的购物车数据
//        Long currentId = BaseContext.getCurrentId();
//        shoppingCart.setUserId(currentId);
//
//        Long dishId = shoppingCart.getDishId();
//
//        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId,currentId);
//
//        if(dishId != null){
//            //添加到购物车的是菜品
//            queryWrapper.eq(ShoppingCart::getDishId,dishId);
//
//        }else{
//            //添加到购物车的是套餐
//            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
//        }
//
//        //查询当前菜品或者套餐是否在购物车中
//        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
//        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
//
//        if(cartServiceOne != null){
//            //如果已经存在，就在原来数量基础上加一
//            Integer number = cartServiceOne.getNumber();
//            cartServiceOne.setNumber(number + 1);
//            shoppingCartService.updateById(cartServiceOne);
//        }else{
//            //如果不存在，则添加到购物车，数量默认就是一
//            shoppingCart.setNumber(1);
//            shoppingCart.setCreateTime(LocalDateTime.now());
//            shoppingCartService.save(shoppingCart);
//            cartServiceOne = shoppingCart;
//        }
//
//        return cartServiceOne;
//    }



}
